package jobteam.contacteventstocalendar;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.provider.ContactsContract.RawContacts.Data;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class hasEventContactsList extends Activity {
	private String TAG = "hasEventContactsList";

	private ListView mList;
	private ArrayList<Integer> mArrayList;
	private ArrayList<String> mNameList;
	private ArrayAdapter<String> mAdapter;
	private ArrayList<ContactIDandName> mIDandNAME;

	private Context m_Context;
	private static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
		ContactsContract.Data.RAW_CONTACT_ID 
	};

	private static final String[] CONTACTS_SUMMARY_PROJECTION2 = new String[] {
		ContactsContract.Data.DATA1,
		ContactsContract.Data.MIMETYPE,
		ContactsContract.Data.RAW_CONTACT_ID
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		if(mArrayList.size() == 0){
			Log.d(TAG, "Has no Contacts");
			return false;
		}
		menu.add(0, 4, 0, R.string.Menu_Lunar);
		menu.add(0, 1, 0, R.string.Menu_Add_Year);
		menu.add(0, 2, 0, R.string.Menu_Add_Once);
		menu.add(0, 3, 0, R.string.Menu_Edit);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		SparseBooleanArray selectedPosArr = mList.getCheckedItemPositions();
		if(selectedPosArr.size() == 0){
			Log.d(TAG, "Has no selected Contact");
			Toast.makeText(this, R.string.no_Selected_Item, Toast.LENGTH_SHORT).show();
			return false;
		}

		int SelectedItemSize = selectedPosArr.size();
		ArrayList<Integer> SelectedItemPos = new ArrayList<Integer>();
		for(int i = 0 ; i < SelectedItemSize ; i++){
			SelectedItemPos.add(selectedPosArr.keyAt(i));
		}

		ArrayList<Integer> SelectedContactIDArr = new ArrayList<Integer>();
		//ArrayList<String> SelectedContactNameArr = new ArrayList<String>();
		for(int i = 0 ; i < SelectedItemSize ; i++){
			SelectedContactIDArr.add(mIDandNAME.get(SelectedItemPos.get(i)).ContactID);
		}

		switch(item.getItemId()){
		case 1:
			
			Intent yearlyintent = 
				new Intent("android.intent.action.INSERT_EVENT").setClass(this, InsertContactsEventToCalendarActivity.class);
			yearlyintent.putExtra("ContactIDArr", SelectedContactIDArr);
			yearlyintent.putExtra("mode", "addYearly");
			startActivity(yearlyintent);
			return true;
		case 2:
			Intent dailyintent = 
				new Intent("android.intent.action.INSERT_EVENT").setClass(this, InsertContactsEventToCalendarActivity.class);
			dailyintent.putExtra("ContactIDArr", SelectedContactIDArr);
			dailyintent.putExtra("mode", "addOnce");
			startActivity(dailyintent);
			return true;
		case 3:
			if(SelectedContactIDArr.size() != 1){
				Toast.makeText(this, R.string.Select_just_One_Item, Toast.LENGTH_SHORT).show();
				onResume();
				return true;
			}
			Intent editintent = new Intent("android.intent.action.EDIT_EVENT").setClass(this, addContactEventActivity.class);
			editintent.putExtra("SelectId", SelectedContactIDArr.get(0));
			startActivity(editintent);
			return true;
		case 4:
			Intent Lunarintent = 
				new Intent("android.intent.action.INSERT_EVENT").setClass(this, InsertContactsEventToCalendarActivity.class);
			Lunarintent.putExtra("ContactIDArr", SelectedContactIDArr);
			Lunarintent.putExtra("mode", "addLunar");
			startActivity(Lunarintent);
			return true;
		}
		return false;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checklistview);
		
		m_Context = this;
		mArrayList = new ArrayList<Integer>();
		mNameList = new ArrayList<String>();
		mIDandNAME = new ArrayList<ContactIDandName>();

	}

	@Override
	protected void onResume() {
		super.onResume();
		mArrayList.clear();
		mNameList.clear();
		mIDandNAME.clear();

		DataCall();
		OriginDataCall();

		if(mArrayList.size() == 0) {
			setContentView(R.layout.nolist);
		}
		else{
			setContentView(R.layout.checklistview);
			for(int i = 0 ; i < mIDandNAME.size() ; i++){
				mNameList.add(mIDandNAME.get(i).ContactName);
			}
			mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mNameList);
			mList = (ListView)findViewById(R.id.checklistview);

			mList.setAdapter(mAdapter);
			mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);   
		}

		if(mArrayList.size() == 0) {
			setContentView(R.layout.nolist);

			new AlertDialog.Builder(this)
			.setTitle("Information")
			.setMessage(R.string.has_no_Event_ContactsList)
			.setIcon(R.drawable.icon)
			.setCancelable(false)
			.setPositiveButton(R.string.Confirm, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton){

				}
			}).show();
		}
	}

	public ArrayList<Integer> DataCall(){
		Cursor cursor = ContactDBCursor();
		if(cursor.moveToFirst()){
			for(int i=0 ; i<cursor.getCount() ; i++){
				int rawId = 0;
				rawId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
				mArrayList.add(rawId);
				cursor.moveToNext();
			}
		}
		return mArrayList;
	}

	public Cursor ContactDBCursor(){

		Uri ContactBaseUri = ContactsContract.Data.CONTENT_URI;
		Cursor cusor = m_Context.getContentResolver().query(ContactBaseUri, 
				CONTACTS_SUMMARY_PROJECTION, 
				Data.MIMETYPE + " LIKE " + "'" + Event.CONTENT_ITEM_TYPE + "'", null, null);

		return cusor;
	}

	public void OriginDataCall(){
		Cursor cursor = ContactOriginCursor();
		if(cursor.moveToFirst()){
			for(int i=0 ; i<cursor.getCount() ; i++){
				String str = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));
				if(str.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)){
					int rawContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
					String ContactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DATA1));
					if(ContactName != null){
						ContactIDandName tmpIDNAME = new ContactIDandName(rawContactID, ContactName);
						mIDandNAME.add(tmpIDNAME);
					}
				}
				cursor.moveToNext();
			}
		}
	}

	public Cursor ContactOriginCursor(){

		Uri ContactBaseUri = ContactsContract.Data.CONTENT_URI;
		Cursor cusor = m_Context.getContentResolver().query(ContactBaseUri, 
				CONTACTS_SUMMARY_PROJECTION2, 
				DataSelection(), null, "data1 ASC, raw_contact_id ASC");

		return cusor;
	}

	public String DataSelection(){
		if(mArrayList.size() > 0){
			String test = Data.RAW_CONTACT_ID + " = " + mArrayList.get(0);
			for(int i = 1 ; i < mArrayList.size() ; i++){
				test += " or " + Data.RAW_CONTACT_ID + " = " + mArrayList.get(i);
			}
			return test;
		}
		return null;
	}
}
